	var masterSubjectGrid;

	$(document).ready(function() {
		jQuery("#custbut").jqGrid({
			url : 'getMasterSubjects.htm',
			datatype : "json",
			colNames : [ 'Entity Id', 'Subject Name', 'Description' ],
			colModel : [ {
				name : 'entityId',
				index : 'entityId',
				hidden : true,
				viewable : false,
				editable : false,
				width : 55
			}, {
				name : 'name',
				index : 'name',
				width : 200,
				sortable : true
			}, {
				name : 'description',
				index : 'description',
				width : 200
			} ],
			rowNum : 10,
			rowList : [ 10, 20,30,40,50 ],
			pager : '#pcustbut',
			sortname : 'name',
			multiselect : true,
			rownumbers : true,
			width : 650,
			
			viewrecords : true,
			sortorder : "asc",
			height : 150,
			emptyrecords : "Empty records",
			caption : "Subjects",
			jsonReader : {
				root : "rowList",
				page : "page",
				total : "total",
				records : "record",
				repeatitems : false,
				cell : "cell",
				id : "entityId"
			}
		});
		var deleteOptions = { reloadAfterSubmit:true,
   		 url : "deleteMasterSubjects.htm",
		 onclickSubmit : function(eparams) { 
				var allUserIDs = "";
			    var selectedrows = jQuery("#custbut").jqGrid('getGridParam', 'selarrrow'); 
			    // we can use all the grid methods here
			    // to obtain some data
			    for(var row in selectedrows)
			    	{
			        var userID = 	jQuery('#custbut').jqGrid('getCell',selectedrows[row],'entityId');
			        allUserIDs = allUserIDs + userID + ";" 
			    	}
			    eparams.url = "deleteMasterSubjects.htm" + "?deleteIds=" + allUserIDs; 
			    
			}
};

	jQuery("#custbut").jqGrid('navGrid', '#pcustbut', 
			{ add: false, view: true, del: true, edit: false }
	     ,
    	     {}, // edit options
             {} , // view options
             deleteOptions, // del options
             {}, // search options
             {}  // vew options
              
         );

 jQuery("#custbut").jqGrid('navButtonAdd', '#pcustbut', {
	caption : "Edit",
	onClickButton : function() {
		var gsr = jQuery("#custbut").jqGrid('getGridParam', 'selrow');
		if (gsr) {
			jQuery("#custbut").jqGrid('GridToForm', gsr, "#master-subject-form");
		} else {
			alert("Please select Row")
		}
	}
});

jQuery("#savedata").click(function() {
	var invid = jQuery("#invid").val();
	
	if (invid) {
		jQuery("#custbut").jqGrid('FormToGrid', invid, "#master-subject-form");
		editMasterSubject();
		 document.forms['master-subject-form'].reset();
	} else {
		addMasterSubject();
       
	}
	

});


jQuery("#resetData").click(function() {

	document.forms['master-subject-form'].reset();
});


});


function addNewRow(enityIdValue){

var rids = $('#custbut').jqGrid('getDataIDs');
var newRowId=rids.length+1;

document.forms['master-subject-form'].entityId.value=enityIdValue;
jQuery("#custbut").FormToGrid(newRowId,'#master-subject-form', 'add', 'last');
// jQuery("#custbut").jqGrid('FormToGrid', '#order', 'add', 'last' );
 rids = $('#custbut').jqGrid('getDataIDs');
 document.forms['master-subject-form'].reset();

}
function deleteRows(enityIdValue){

var rids = $('#custbut').jqGrid('getDataIDs');
var newRowId=rids.length+1;

document.forms['master-subject-form'].entityId.value=enityIdValue;
jQuery("#custbut").FormToGrid(newRowId,'#master-subject-form', 'add', 'last');
// rids = $('#custbut').jqGrid('getDataIDs');
 document.forms['master-subject-form'].reset();

}

function addMasterSubject(){

// Create the ajax call
$.ajax({
	type: 'POST',
	url: 'addMasterSubjects.htm',
	data: $("#master-subject-form").serialize(),
	timeout: 5000,
	success: function(data) {
	 if (data) {
		$('#rsperror').html( data.message);
		$('#ajax-message-div').show();
		addNewRow(data.entityId);
	 }	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#ajax-message-div').html("Error creating subject ,Error:"+errorThrown+"<br/>Please refresh the web page ");
	},
	dataType: 'json'
});
}
function editMasterSubject(){

// Create the ajax call
$.ajax({
	type: 'POST',
	url: 'addMasterSubjects.htm',
	data: $("#master-subject-form").serialize(),
	timeout: 5000,
	success: function(data) {
	 if (data) {
		$('#rsperror').html( data.message);
		$('#ajax-message-div').show();
		
	 }	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#ajax-message-div').html("Error creating subject ,Error:"+errorThrown+"<br/>Please refresh the web page ");
	},
	dataType: 'json'
});
}
function deleteMasterSubject(){

// Create the ajax call
$.ajax({
	type: 'GET',
	url: 'deleteMasterSubjects.htm',
	data: $("#master-subject-form").serialize(),
	timeout: 5000,
	success: function(data) {
	 if (data) {
		$('#rsperror').html( data.message);
		$('#ajax-message-div').show();
		deleteRows(data.entityIds);
	 }	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#ajax-message-div').html("Error creating subject ,Error:"+errorThrown+"<br/>Please refresh the web page ");
	},
	dataType: 'json'
   });
}

